﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for CMyStaticFunction
/// </summary>
public class CMyStaticFunction
{
	public CMyStaticFunction()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static bool checkEmailExist(string email, LinQ_RaoVatDataContext linQ)
    {
        int b = linQ.NGUOI_DUNGs.Count(p => p.Email == email);

        if (b > 0)
        {
            return true;
        }

        return false;
    }

     public static bool checkUserNameExist(string tenDangNhap, LinQ_RaoVatDataContext linQ)
    {
        int b = linQ.NGUOI_DUNGs.Count(p => p.TenDangNhap == tenDangNhap);

        if (b > 0)
        {
            return true;
        }

        return false;
    }

     public static bool checkIDExist(string cMND, LinQ_RaoVatDataContext linQ)
    {
        int b = linQ.NGUOI_DUNGs.Count(p => p.SoCMND == cMND);

        if (b > 0)
        {
            return true;
        }

        return false;
    }

    public static bool changePassword(string userName, string oldPassword, string newPassword)
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

        NGUOI_DUNG nd = linQ.NGUOI_DUNGs.Single(p => p.TenDangNhap == userName);

        if (nd == null)
        {
            return false;
        }
        else
        {
            if (nd.MatKhau.Trim() == oldPassword.Trim() && nd.Deleted == false)
            {
                nd.MatKhau = newPassword;
                linQ.SubmitChanges();
                return true;
                // Page_Load(sender, e);
            }
            else
            {
                return false;
            }
            // Page_Load(sender, e);
        }
     
    }

    public static bool getUserNameAndPassWord(string email, out string userName, out string password)
    {

        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

        NGUOI_DUNG nd = linQ.NGUOI_DUNGs.Single(p => p.Email == email);

        if (nd == null)
        {
            userName = "";
            password = "";
            return false;
        }
        else
        {
            userName = nd.TenDangNhap;
            password = nd.MatKhau;
            return true;
        }
    }
    public static int layMaTinRaoVatHot()
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

        int maHot = -1;
        try
        {
           maHot = linQ.LOAI_TIN_RAO_VATs.Single(loaitin => loaitin.TenLoaiTinRaoVat.CompareTo("Hot") == 0 && loaitin.Deleted == false).MaLoaiTinRaoVat;
        }
        catch (System.Exception ex)
        {
        	return -1;
        }
        

        return maHot;
    }

    public static int layMaTinRaoVatThuong()
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

        int maHot = -1;
        try
        {
            maHot = linQ.LOAI_TIN_RAO_VATs.Single(loaitin => loaitin.TenLoaiTinRaoVat.CompareTo("Thường") == 0 && loaitin.Deleted == false).MaLoaiTinRaoVat;
        }
        catch (System.Exception ex)
        {
            return -1;
        }
      

        return maHot;
    }
}
